package org.itboys.mobile.service.mongo.weixin.count;

import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.count.SundayCountDto;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mobile.service.mongo.web.member.SundayMemberService;
import org.itboys.mobile.service.mongo.weixin.address.SundayWeixinAddressService;
import org.itboys.mobile.service.mongo.weixin.cart.CooWeixinCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/7
 * 描述：迈迪龙_微信_结算
 */
@Service
public class CooWeixinCountService {
    @Autowired
    private CooWeixinCartService cartService;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayWeixinAddressService addressService;
    /**
     * 购物车结算。
     * @param memberId
     * @param cartIds
     * @return
     */
    public SundayCountDto countCart(Long memberId, List<Long> cartIds, Integer cartType,  Long addressId){
        SundayCountDto countDto = cartService.getCart(memberId,cartIds,cartType);
        //加载默认地址
        Map<String,Object> address= new HashMap<String, Object>();

        address.put("id", 0);
        address.put("receiveMobile", null);
        address.put("receiveName", null);
        address.put("receiveAddress",null);
        if(LongUtil.isNotZreo(addressId)){
            SundayAddress add = addressService.getById(addressId);
            address.put("id", add.getId());
            address.put("receiveMobile", add.getReceiveMobile());
            address.put("receiveName", add.getReceiveName());
            address.put("receiveAddress", add.getReceiveAddress());
            //countDto.setAddress(address);
        }else{
            SundayAddress defaultAdd = addressService.getDefaultAddress(memberId);
            if(defaultAdd!=null){
                address.put("id", defaultAdd.getId());
                address.put("receiveMobile", defaultAdd.getReceiveMobile());
                address.put("receiveName", defaultAdd.getReceiveName());
                address.put("receiveAddress", defaultAdd.getReceiveAddress());
            }
        }
        countDto.setAddress(address);
        return countDto;
    }

    /**
     * 立即购买结算
     * @param memberId
     * @param productId
     * @param number
     * @return
     */
    public SundayCountDto countBuyNow(Long memberId,Long productId,Integer number,Long shopId,Long addresssId){
        List<Long> productIds =new ArrayList<Long>();
        productIds.add(productId);
        List<Integer> numbers =new ArrayList<Integer>();
        numbers.add(number);
        List<Long> cartIds= cartService.addCart(memberId,productIds ,numbers, SundayCommonConstants.cart_type_buy_now);
        return countCart(memberId,cartIds,SundayCommonConstants.cart_type_buy_now,addresssId);
    }

}
